//
// Created by mac on 1/26/22.
//
#include <string>
using namespace std;

int myAtoi(string s) {
    int n = s.size();
    if(n==0){ return 0;}

    int i = 0;
    //空格
    while (i<n && s[i]==' '){
        i++;
    }
    if(i>=n){ return 0;}

    //判断正负
    int sign = 1;
    if(s[i]=='-'|| s[i]=='+'){
        sign = s[i] == '-' ? -1 : 1;
        i++;
    }

    int base = 0;
    while (i<n && s[i]>='0' && s[i]<='9'){

        //判断是否越界
        if(base>INT_MAX/10 ||(base==INT_MAX/10 && s[i]-'0'>INT_MAX%10)){
            if(sign==1){ return INT_MAX;}
            else{ return INT_MIN;}
        }
        base = base*10+(s[i]-'0');
        i++;
    }
    return base*sign;
}